package com.mugcode.hosp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mugcode.common.result.Result;
import com.mugcode.common.utils.MD5;
import com.mugcode.entity.model.hosp.HospitalSet;
import com.mugcode.entity.vo.hosp.HospitalSetQueryVo;
import com.mugcode.hosp.service.HospitalSetService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * projectName: appointment
 *
 * @author: Mu
 * time: 2023/5/21 11:05
 * description:
 */
@Api(tags="医院设置管理")
@RestController
@CrossOrigin
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;

    //查询医院设置表的信息
    @ApiOperation(value="获取所有医院设置")
    @GetMapping("findAll")
    public Result findAllHospitalSet(){
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }
    //2 逻辑删除医院设置
    @ApiOperation(value = "逻辑删除医院设置")
    @DeleteMapping("{id}")
    public Result removeHospSet(@PathVariable Long id) {
        boolean flag = hospitalSetService.removeById(id);
        if(flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    //条件查询带分页
    @ApiOperation(value = "条件查询医院分页显示")
    @PostMapping(value="findPageHospSet/{current}/{limit}")
    public Result findPageHospSet(@ApiParam("当前页") @PathVariable String current,
                                  @ApiParam("每页记录条数") @PathVariable String limit,
                                  @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo){
        //创建page对象, 传递当前页, 每页记录数
        Page<HospitalSet> page = new Page<>(Long.parseLong(current), Long.parseLong(limit));
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        String hospName = hospitalSetQueryVo.getHosname();
        String hospCode = hospitalSetQueryVo.getHoscode();
        if(!StringUtils.isEmpty(hospName)){
            wrapper.like("hosname", hospName);
        }
        if(!StringUtils.isEmpty(hospCode)){
            wrapper.eq("hoscode",hospCode);
        }

        IPage<HospitalSet> pageHospitalSet = hospitalSetService.page(page, wrapper);
        return Result.ok(pageHospitalSet);
    }

    //添加医院设置
    @ApiOperation(value="添加医院")
    @PostMapping("saveHospitalSet")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet){
        //设置状态 1使用 0不能使用
        hospitalSet.setStatus(1);
        //签名密钥
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000)));
        return hospitalSetService.save(hospitalSet) ? Result.ok() :  Result.fail();
    }

    //根据id获取医院设置
    @ApiOperation(value="根据id获取医院设置")
    @GetMapping("getHospSet/{id}")
    public Result getHospSet(@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.ok(hospitalSet);
    }

    //修改医院设置
    @PostMapping("updateHospitalSet")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet){
        boolean flag = hospitalSetService.updateById(hospitalSet);
        return flag ? Result.ok() :  Result.fail();
    }

    //批量删除
    @DeleteMapping("batchRemove")
    public Result batchRemoveHospitalSet(@RequestBody List<Long> idList){
        return hospitalSetService.removeByIds(idList) ? Result.ok() : Result.fail();
    }

    //医院设置锁定和解锁
    @PutMapping("lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet (@PathVariable Long id,
                                   @PathVariable Integer status){
        //根据id查询医院设置信息
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        //设置状态 1解锁 0不可用
        if(status == 1 || status == 0) {
            hospitalSet.setStatus(status);
        }
        hospitalSetService.updateById(hospitalSet);
        return Result.ok();
    }

    //发送签名密钥
    @PutMapping("sendKey/{id}")
    public Result lockHospitalSet (@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hascode = hospitalSet.getHoscode();
        return Result.ok();
    }

}
